package com.avic.rong.fragment.balance;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.avic.rong.R;
import com.avic.rong.activity.mine.CapitalSubsidiaryActivity;
import com.avic.rong.adapter.balance.RecvBzMemberBalanceAdapter;
import com.avic.rong.api.mine.BzMemberCardApi;
import com.avic.rong.bean.mine.BzMemberCardBean;
import com.avic.rong.fragment.BaseLazyFragment;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class BzMemberChildFragment extends Fragment implements OnRefreshLoadMoreListener {

    @BindView(R.id.recv_bzmember)
    RecyclerView recv_bzmember;
    @BindView(R.id.srl_bzmember)
    SmartRefreshLayout srl_bzmember;
    private String title;
    private Context context;
    private Integer integer;
    private String loginToken;
    private View view;
    private Unbinder unbinder;
    private Integer pageNumber = 1;
    private BzMemberCardApi bzMemberCardApi;
    private ArrayList<BzMemberCardBean> mList = new ArrayList<>();
    private RecvBzMemberBalanceAdapter recvBzMemberBalanceAdapter;
    private boolean isRefresh;
    private LoadingLayout loading_layout;

    public BzMemberChildFragment() {
        // Required empty public constructor
    }



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        initData();
    }



    public static BzMemberChildFragment getInstance(String title, Context context, Integer integer, String loginToken) {
        BzMemberChildFragment bzMemberFragment = new BzMemberChildFragment();
        bzMemberFragment.title = title;
        bzMemberFragment.context = context;
        bzMemberFragment.integer = integer;
        bzMemberFragment.loginToken = loginToken;
        return bzMemberFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_bz_member_child, container, false);

        unbinder = ButterKnife.bind(this, inflate);

        loading_layout = inflate.findViewById(R.id.loading_layout);
        loading_layout.setStatus(LoadingLayout.LOADING);


        loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                HttpManager.getInstance().doHttpDeal(bzMemberCardApi);
                loading_layout.setStatus(LoadingLayout.LOADING);
            }

            @Override
            public void onReturn(View v) {
                getActivity().onBackPressed();
            }
        });

        recv_bzmember.setLayoutManager(new LinearLayoutManager(getContext()));
        recvBzMemberBalanceAdapter = new RecvBzMemberBalanceAdapter(mList, getContext());
        recv_bzmember.setAdapter(recvBzMemberBalanceAdapter);



        srl_bzmember.setOnRefreshLoadMoreListener(this);

        return inflate;
    }

    private void initData() {
        bzMemberCardApi = new BzMemberCardApi((CapitalSubsidiaryActivity) getActivity(), bzMemberCardListener);
        bzMemberCardApi.setCache(false);
        bzMemberCardApi.setArgument(loginToken,pageNumber,10,integer);
        HttpManager.getInstance().doHttpDeal(bzMemberCardApi);
    }

    HttpOnNextListener<BaseResultEntity> bzMemberCardListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                loading_layout.setStatus(LoadingLayout.SUCCESS);
                List<BzMemberCardBean> data = (List<BzMemberCardBean>) baseResultEntity.getData();

                if (isRefresh) {
                    mList.clear();
                }
                mList.addAll(data);
                recvBzMemberBalanceAdapter.notifyDataSetChanged();
                if (isRefresh) {
                    srl_bzmember.finishRefresh();
                } else {
                    srl_bzmember.finishLoadMore();
                }
                if (mList!=null&&mList.size()<=0){
                    loading_layout.setStatus(LoadingLayout.EMPTY);
                    loading_layout.setEmptyImageVisible(true);
                    loading_layout.setEmptyImage(R.mipmap.empty_capital);
                    loading_layout.setEmptyText("暂无资金明细");
                }
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
        }
        @Override
        public void onError(Throwable e) {
            super.onError(e);
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isRefresh = false;
        pageNumber = pageNumber + 1;
        bzMemberCardApi.setPageNumber(pageNumber);
        HttpManager.getInstance().doHttpDeal(bzMemberCardApi);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isRefresh = true;
        pageNumber = 1;
        bzMemberCardApi.setPageNumber(pageNumber);
        HttpManager.getInstance().doHttpDeal(bzMemberCardApi);
    }

}
